package com.wanlink.mall.app.presenter;

import com.wanlink.mall.app.model.BannerInfo;
import com.wanlink.mall.app.model.BaseModel;
import com.wanlink.mall.app.ui.view.HomeView;
import com.wanlink.mall.base.BasePresenter;
import com.wanlink.mall.http.Exception.ApiException;
import com.wanlink.mall.http.HttpManager;
import com.wanlink.mall.http.response.BaseResponse;
import com.wanlink.mall.http.rxhelper.RxObserver;
import com.wanlink.mall.http.rxhelper.RxResultHelper;
import com.wanlink.mall.http.rxhelper.RxSchedulersHelper;

import java.util.List;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;

public class HomePresenter extends BasePresenter<HomeView> {

    public void getBanner() {
        HttpManager.create()
                .getRecommendBannerInfo()
                .compose(RxResultHelper.handleResult())
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<List<BannerInfo>>() {
                    @Override
                    public void _onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                        if (getView() != null) {
                            getView().showLoading();
                        }

                    }

                    @Override
                    public void _onNext(@NonNull List<BannerInfo> list) {
                        getView().showToast(list.size() + "");
                    }

                    @Override
                    public void _onError(ApiException e) {
                        getView().showToast(e.getMsg());
                    }

                    @Override
                    public void _onComplete() {
                        if (getView() != null) {
                            getView().dismissLoading();
                        }
                    }
                });


    }
}
